//
//  HEIMForward.m
//  HealthElderly
//
//  Created by 吴欧 on 2020/11/27.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEIMForward.h"
#import "HEIMMessageModel.h"
#import "HEIMImageObject.h"
#import "HEIMAudioObject.h"
#import "HEIMVideoObject.h"
#import "HETWFormObject.h"
#import "HEIMProductObject.h"
#import "HEIMPrivateDoctorObject.h"
#import "HEIMATObject.h"

@implementation HEIMForward

+ (NSArray <HEIMMessageModel *> *)transformIMModel:(NSArray <HEIMInfoEntity *>*)imInfoList {
    NSMutableArray *trsArr = @[].mutableCopy;
    for (HEIMInfoEntity *item in imInfoList) {
        if ([DYTools isBlankString:item.content]) {// 私人医生签约消息解析
            HEIMPrivateDoctorObject *private = [HEIMPrivateDoctorObject mj_objectWithKeyValues:item.extras];
            
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.type = HEIMMessageTypeText;
            message.text = private.disease_desc;
            message.from = item.from_id;
            message.to = item.to;
            [self setupModelTypeItem:item message:message];
            
            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = HEIMMessageDeliveryStateDeliveried;
            [trsArr addObject:model];
        }else {
            [trsArr addObject:[self matchMessage:item deliveryState:HEIMMessageDeliveryStateDeliveried]];
        }
    }
    return trsArr;
}

+ (HEIMMessageModel *)matchMessage:(HEIMInfoEntity *)item deliveryState:(HEIMMessageDeliveryState)deliveryState {
    
    HEIMInfoContentEntity *contentModel = item.contentModel;
    switch (contentModel.type) {
        case HEIMMessageTypeText:
        {
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.type = HEIMMessageTypeText;
            message.text = contentModel.content;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.to = item.to;
            [self setupModelTypeItem:item message:message];
            
            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
            return model;
        }
            break;
        case HEIMMessageTypeImage:
        {
            HEIMImageObject *image = [[HEIMImageObject alloc] init];
            image.imageArray = [self imageUrlToArray: contentModel.content networkType: deliveryState!=HEIMMessageDeliveryStateDeliveried ? HEIMImageSourceTypeNative : HEIMImageSourceTypeWebUrl];
            
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.type = HEIMMessageTypeImage;
            message.messageObject = image;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.to = item.to;
            [self setupModelTypeItem:item message:message];

            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage: message];
            model.deliveryState = deliveryState;

            return model;
        }
            break;
        case HEIMMessageTypeAudio:
        {
            HEIMAudioObject *audio = [HEIMAudioObject new];
            audio.url = contentModel.content;
            audio.duration = contentModel.seconds;
            
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.type = HEIMMessageTypeAudio;
            message.messageObject = audio;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.to = item.to;
            [self setupModelTypeItem:item message:message];

            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;

            return model;
        }
            break;
        case HEIMMessageTypeProduct:
        {
      
            HEIMProductObject *product = [HEIMProductObject new];
            product.product_id = contentModel.product_id;
            product.product_name = contentModel.product_name;
            product.product_icon = contentModel.product_icon;
            product.product_price = contentModel.product_price;
            product.serviceType = contentModel.serviceType;
            product.outOriginalId = contentModel.outOriginalId;
            product.rebate = contentModel.rebate;
            
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.type = HEIMMessageTypeProduct;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.messageObject = product;
            message.to = item.to;
            [self setupModelTypeItem:item message:message];

            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
            return model;
        }
            break;
            
        case HEIMMessageTypeConsulReport:
        {
      
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.type = HEIMMessageTypeConsulReport;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.title = contentModel.reportTitle;
            message.text = contentModel.content;
            message.to = item.to;
            [self setupModelTypeItem:item message:message];

            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
            return model;
        }
            break;
        case HEIMMessageTypeComment:
        {
            HEIMCommentObject *comment = [HEIMCommentObject new];
            comment.score = contentModel.score;
            
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.text = contentModel.content;
            message.type = HEIMMessageTypeComment;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.messageId = item.id;
            message.to = item.to;
            message.messageObject = comment;
            [self setupModelTypeItem:item message:message];

            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
            return model;
        }
            break;
            
        case HEIMMessageTypeAT:
        {
            HEIMATObject *comment = [HEIMATObject new];
            comment.atIds = contentModel.atIds;
            
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.text = contentModel.content;
            message.type = HEIMMessageTypeAT;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.to = item.to;
            message.messageObject = comment;
            [self setupModelTypeItem:item message:message];

            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
            return model;
        }
            break;
            
        case HEIMMessageTypeInfoPage:
        {
            
            HEIMInfoPageContentEntity *pageInfoEntity = [HEIMInfoPageContentEntity mj_objectWithKeyValues:[contentModel.content mj_JSONObject]];
            HEIMInfoPageObject *infoPage = [HEIMInfoPageObject new];
            infoPage.id = pageInfoEntity.id;
            infoPage.voiceUrl = pageInfoEntity.voiceUrl;
            infoPage.iconUrlStr = pageInfoEntity.icon;
            infoPage.iconURL = [NSURL URLWithString:pageInfoEntity.icon];
            infoPage.title = pageInfoEntity.title;
            infoPage.infoUrl = pageInfoEntity.infoUrl;
            infoPage.subTitle = pageInfoEntity.subTitle;
 
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.text = contentModel.content;
            message.type = HEIMMessageTypeInfoPage;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.to = item.to;
            message.messageObject = infoPage;
            [self setupModelTypeItem:item message:message];

            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
            return model;
        }
            break;
            
        case HEIMMessageTypeInfoVideo:
        {
            
            HEIMInfoPageContentEntity *pageInfoEntity = [HEIMInfoPageContentEntity mj_objectWithKeyValues:[contentModel.content mj_JSONObject]];
            HEIMInfoPageObject *infoPage = [HEIMInfoPageObject new];
            infoPage.id = pageInfoEntity.id;
            infoPage.voiceUrl = pageInfoEntity.voiceUrl;
            infoPage.iconUrlStr = pageInfoEntity.icon;
            infoPage.iconURL = [NSURL URLWithString:pageInfoEntity.icon];
            infoPage.title = pageInfoEntity.title;
            infoPage.infoUrl = pageInfoEntity.infoUrl;
            infoPage.subTitle = pageInfoEntity.subTitle;
 
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.text = contentModel.content;
            message.type = HEIMMessageTypeInfoVideo;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.to = item.to;
            message.messageObject = infoPage;
            [self setupModelTypeItem:item message:message];

            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
            return model;
        }
            break;
        case HEIMMessageTypeGroupNotice:
        {
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.type = HEIMMessageTypeGroupNotice;
            message.text = contentModel.content;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.to = item.to;
            [self setupModelTypeItem:item message:message];
            
            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
            return model;
        }
            break;
            
        case HEIMMessageTypeGroupNotif:
        {
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.type = HEIMMessageTypeGroupNotif;
            message.text = [NSString stringWithFormat:@"群公告\n%@", contentModel.content];
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.to = item.to;
            [self setupModelTypeItem:item message:message];
            
            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
            return model;
        }
            break;
            
        default:
        {
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.type = HEIMMessageTypeText;
            message.text = contentModel.content;
            message.from = item.from_id;
            message.actor = contentModel.actor;
            message.to = item.to;
            [self setupModelTypeItem:item message:message];

            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;

            return model;
        }
            break;
    }
    return nil;
}

+ (void)setupModelTypeItem:(HEIMInfoEntity *)item message:(HEIMMessage *)message {
    if ([@"0" isEqualToString:item.msgType]) {
        message.modelType = HEIMUserModelTypeIM;
    }else if ([@"1" isEqualToString:item.msgType]){
        message.modelType = HEIMUserModelTypeTeamIM;
    }else if ([@"3" isEqualToString:item.msgType]) {
        message.modelType = HEIMUserModelTypeSickbed;
    }else if ([@"4" isEqualToString:item.msgType]) {
        message.modelType = HEIMUserModelTypeGroup;
    }else {
        message.modelType = HEIMUserModelTypeRoom;
    }
}

+ (NSArray <HEIMMessageModel *> *)transformNoSendIMModel:(NSArray <HEIMInfoEntity *>*)imInfoList {
    NSMutableArray *trsArr = @[].mutableCopy;
    for (HEIMInfoEntity *item in imInfoList) {
        [trsArr addObject:[self matchMessage:item deliveryState: HEIMMessageDeliveryStateDelivering]];
    }
    return trsArr;
}

+ (NSArray <HEIMImageObject *>*)imageUrlToArray:(NSString *)urls networkType:(HEIMImageSourceType)type {
    NSArray *arr = [urls componentsSeparatedByString:@","];
    NSMutableArray *imgs = @[].mutableCopy;
    for (NSString *url in arr) {
        if (![DYTools isBlankString:url]) {
            HEIMImageObject *image = [[HEIMImageObject alloc] init];
            
            if (type == HEIMImageSourceTypeNative) {
                image.url = [NSURL fileURLWithPath:url];
            }else {
                image.url = [NSURL URLWithString:url];
            }
            image.imageSourceType = type;
            [imgs addObject: image];
        }
    }
    return imgs;
}

#pragma mark - tw
+ (NSArray <HEIMMessageModel *> *)transformTWModel:(NSArray <HEDocImMsgModel *>*)imTWList {
    NSMutableArray *trsArr = @[].mutableCopy;
    UserInfo *user = [UserManager sharedUserManager].getUserInfo;
    
    for (HEDocImMsgModel *item in imTWList) {
        
        item.from = item.isMe ? user.id : item.doc_id;
        HETWIMInfoModel *contentModel = item.twContentModel;

        if (item.formContents.count > 0) {
            HETWFormObject *twForm = [HETWFormObject new];
            NSArray<HEIMMessageModel *> *extractedExpr = [self twFormsToIMMessages: item isForm: YES];
            twForm.formContents = extractedExpr;
    
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.refrenceId = item.inst_id;
            message.modelType = HEIMUserModelTypeConsul;
            message.type = HEIMMessageTypeTWForm;
            message.messageObject = twForm;
            message.from = item.from;
            HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = HEIMMessageDeliveryStateDeliveried;
            [trsArr addObject:model];
        }else {
            [trsArr addObject:[self matchTWMessage:contentModel withModel:item deliveryState:HEIMMessageDeliveryStateDeliveried isForm: NO]];
        }
    }
    return trsArr;
}

+ (NSArray <HEIMMessageModel *> *)transformNoSendTWModel:(NSArray <HEDocImMsgModel *>*)imTWList {
    NSMutableArray *trsArr = @[].mutableCopy;
    UserInfo *user = [UserManager sharedUserManager].getUserInfo;

    for (HEDocImMsgModel *item in imTWList) {
        item.from = item.isMe ? user.id : item.doc_id;
        HETWIMInfoModel *contentModel = item.twContentModel;
        
        [trsArr addObject:[self matchTWMessage:contentModel withModel:item deliveryState:HEIMMessageDeliveryStateDelivering isForm:NO]];
    }
    return trsArr;
}

+ (NSArray <HEIMMessageModel *> *)twFormsToIMMessages:(HEDocImMsgModel *)model isForm:(BOOL)isForm{
    NSMutableArray *trsArr = @[].mutableCopy;
    for (HETWIMInfoModel *item in model.formContents) {
        [trsArr addObject:[self matchTWMessage:item withModel:model deliveryState:HEIMMessageDeliveryStateDeliveried isForm:isForm]];
    }
    return trsArr;
}

+ (HEIMMessageModel *)matchTWMessage:(HETWIMInfoModel *)contentModel withModel:(HEDocImMsgModel *)item deliveryState:(HEIMMessageDeliveryState)deliveryState isForm:(BOOL)isForm{
    
    if ([TWIM_TEXT isEqualToString: contentModel.type]) {
        HEIMMessage *textMessage = [[HEIMMessage alloc] init];
        textMessage.modelType = HEIMUserModelTypeConsul;
        textMessage.type = HEIMMessageTypeText;
        textMessage.text = contentModel.content;
        textMessage.from = item.from;
        textMessage.to = item.doc_id;
        textMessage.refrenceId = item.inst_id;
        HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:textMessage];
        model.deliveryState = deliveryState;
        return model;
    }
    
    else if ([TWIM_IMAGE isEqualToString: contentModel.type]) {
        HEIMImageObject *image = [[HEIMImageObject alloc] init];
        image.imageArray = [self imageUrlToArray: contentModel.content networkType: deliveryState!=HEIMMessageDeliveryStateDeliveried ? HEIMImageSourceTypeNative : HEIMImageSourceTypeWebUrl];
        
        HEIMMessage *message = [[HEIMMessage alloc] init];
        message.modelType = HEIMUserModelTypeConsul;
        message.type = HEIMMessageTypeImage;
        message.messageObject = image;
        message.from = item.from;
        message.to = item.doc_id;
        message.refrenceId = item.inst_id;
        HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage: message];
        model.deliveryState = deliveryState;

        return model;
    }
    else if ([TWIM_AUDIO isEqualToString: contentModel.type]) {
        HEIMAudioObject *audio = [HEIMAudioObject new];
        audio.url = contentModel.content;
        audio.duration = contentModel.seconds;
        
        HEIMMessage *message = [[HEIMMessage alloc] init];
        message.modelType = HEIMUserModelTypeConsul;
        message.type = HEIMMessageTypeAudio;
        message.messageObject = audio;
        message.from = item.from;
        message.to = item.doc_id;
        message.refrenceId = item.inst_id;
        HEIMMessageModel *model = [[HEIMMessageModel alloc] initWithMessage:message];
        model.deliveryState = deliveryState;

        return model;
    }
    else if ([TWIM_VIDEO isEqualToString: contentModel.type]) {
        
        HEIMMessageModel *model = nil;
        if (isForm) {
            HEIMVideoObject *video = [HEIMVideoObject new];
            NSArray *arr = [contentModel.content componentsSeparatedByString:@","];
            NSMutableArray *imgs = @[].mutableCopy;
            for (NSString *url in arr) {
                if (![DYTools isBlankString:url]) {
                    [imgs addObject:url];
                }
            }
            video.urls = imgs;
            
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.modelType = HEIMUserModelTypeConsul;
            message.type = HEIMMessageTypeVideo;
            message.messageObject = video;
            message.from = item.from;
            message.to = item.doc_id;
            message.refrenceId = item.inst_id;
            model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
        }else {
            HEIMVideoObject *video = [HEIMVideoObject new];
            video.sourceType = deliveryState!=HEIMMessageDeliveryStateDeliveried ? HEIMMediaSourceTypeNative : HEIMMediaSourceTypeWebUrl;
            video.url = [self mediaUrlToArray: contentModel.content networkType: video.sourceType];
            
            HEIMMessage *message = [[HEIMMessage alloc] init];
            message.modelType = HEIMUserModelTypeConsul;
            message.type = HEIMMessageTypeVideo;
            message.messageObject = video;
            message.from = item.from;
            message.to = item.doc_id;
            message.refrenceId = item.inst_id;
            model = [[HEIMMessageModel alloc] initWithMessage:message];
            model.deliveryState = deliveryState;
        }
        return model;
    }
     
    return nil;
}
 
+ (NSURL *)mediaUrlToArray:(NSString *)urls networkType:(HEIMMediaSourceType)type {
    NSArray *arr = [urls componentsSeparatedByString:@","];
    for (NSString *url in arr) {
        if (![DYTools isBlankString:url]) {
            NSURL *pathUrl = nil;
            if (type == HEIMMediaSourceTypeNative) {
                pathUrl = [NSURL fileURLWithPath:url];
            }else {
                pathUrl = [NSURL URLWithString:url];
            }
            return pathUrl;
        }
    }
    return nil;
}

+ (NSString *)getMsgType:(HEIMUserModelType)modelType {
    switch (modelType) {
        case HEIMUserModelTypeIM:
            return @"0";
        break;
        case HEIMUserModelTypeTeamIM:
            return @"1";
        break;
        case HEIMUserModelTypeSickbed:
            return @"3";
        break;
        case HEIMUserModelTypeGroup:
            return @"4";
        break;
    }
    return @"";
}

@end
